<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.TitleDAO" %>

<%@page import="db.GroupDAO"%>
<%@page import="db.Material"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>

<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	MaterialDAO materialDao = (MaterialDAO)ctx.getBean("MaterialDAO");
	String code = request.getParameter("code");
	String codename = request.getParameter("codename");
	String codecompany = request.getParameter("codecompany");
	String type = request.getParameter("type");
	request.setAttribute("type",type);
	String message = "";
	if(code==null || "".equals(code)){
			message = "物资编码关联失败，物资编码不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/materials/addMaterials.vm");
			return;
	}
	
	int tid = (Integer)session.getAttribute("tid");
	List md = materialDao.findCodeAndTeamId(code,tid);
	if(md != null && md.size() > 0){
		message = "物资编码关联失败，物资编码已经存在。";
		request.setAttribute("suess",false);
		request.setAttribute("message",message);
		pageContext.forward("/vm/materials/addMaterials.vm");
		return;
	}
	//List md = materialDao.findCodeAndTeamId(codename,tid);
	if(codename==null || "".equals(codename)){
			message = "物资编码关联失败，物资名称不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/materials/addMaterials.vm");
			return;
	}
	
	List mdn = materialDao.findCodeNameAndTeamId(codename,tid);
	if(mdn != null && mdn.size() > 0){
		message = "物资编码关联失败，物资名称已经存在。";
		request.setAttribute("suess",false);
		request.setAttribute("message",message);
		pageContext.forward("/vm/materials/addMaterials.vm");
		return;
	}
	if(codecompany==null || "".equals(codecompany)){
			message = "物资编码关联失败，物资单位不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/materials/addMaterials.vm");
			return;
	}
	Material ma = new Material();
	ma.setCode(code);
	ma.setCompany(codecompany);
	ma.setDate(new Date());
	ma.setName(codename);
	ma.setTeamId(tid);
	ma.setIsDelete(0);
	materialDao.save(ma);
	message = "恭喜您！物资编码关联成功!";
	request.setAttribute("message",message);
	request.setAttribute("suess",true);
	
	pageContext.forward("/vm/materials/addMaterials.vm");
%>
